package cn.tedu.test.security;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
// @EnableWebSecurity SpringBoot 无需配置
// 继承 WebSecurityConfigurerAdapter 添加Web权限配置
@EnableGlobalMethodSecurity(prePostEnabled = true) //开启方法访问授权
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //.mvcMatchers("/images/**").permitAll() 通过安全过滤器. 被放过了
        http.authorizeRequests()
                .mvcMatchers("/images/**").permitAll()
                .mvcMatchers("/admin/**").hasRole("ADMIN")
                .mvcMatchers("/home/**").hasAnyRole("USER","ADMIN")
                .anyRequest().authenticated()
                .and().formLogin();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //忽略 /images/** 连接, 绕过了 web 安全过滤器
        web.ignoring().mvcMatchers("/images/**", "/actuator/**");

    }
}
